<template>
  <div class="device-parameter-container">
    <header-search-form
      :searchFormData="searchForm"
      @search="search"
    />
    <lan-table
      size="small"
      :config="dataTable"
      :tableData="tableData"
      @search="search"
    >
      <template #type="scope">
        <el-input v-model="scope.row.type"></el-input>
      </template>
    </lan-table>
    <new-edit
      ref="editComponent"
      @search="search"
    />
  </div>
</template>

<script>
import lanTable from '../../../components/lanTable';
import newEdit from './newEdit';
import HeaderSearchForm from './headerSearchForm.vue';

import moment from 'moment';
import { MomentFormat, SortType } from '../../../constants/common';
import { Message, MessageBox } from 'element-ui';
import { getDeviceParameterListApi, deleteDeviceParameterApi } from '../../../api/deviceParameter';
import { parseError } from '../../../utils';

export default {
  components: {
    lanTable,
    newEdit,
    HeaderSearchForm
  },
  data() {
    return {
      searchForm: {
        btnLoading: false,
        name: ''
      },
      tableData: [],
      dataTable: {
        sort: {
          orderBy: 'createdAt',
          sortType: SortType.DESC
        },
        pageSize: 10,
        totalCount: 0,
        currentPage: 1,
        pageSizes: [5, 10, 20, 50, 100],

        columnList: [
          {
            name: 'index',
            label: '',
            type: 'index'
          },
          {
            name: 'name',
            label: '参数名称'
          },
          {
            name: 'displayName',
            label: '显示名称'
          },
          {
            name: 'unit',
            label: '参数单位'
          },
          {
            name: 'description',
            label: '参数描述'
          },
          {
            name: 'createdAt',
            label: '创建时间',
            sortable: true
          },
          {
            name: 'updateAt',
            label: '更新时间'
          },
          {
            type: 'button',
            label: '操作',
            buttons: [
              {
                type: 'text',
                text: '编辑',
                size: 'small',
                method: row => {
                  this.showEdit(row);
                }
              },
              {
                type: 'text',
                text: '删除',
                size: 'small',
                method: row => {
                  this.remove(row.id, row.name);
                }
              }
            ]
          }
        ]
      }
    };
  },
  created() {
    this.search();
  },
  methods: {
    search() {
      this.searchForm.btnLoading = true;
      getDeviceParameterListApi(this.dataTable.currentPage - 1, this.dataTable.pageSize, this.dataTable.sort.sortType, this.dataTable.sort.orderBy, this.searchForm.name)
        .then(({ data, total_count }) => {
          this.dataTable.totalCount = total_count;
          this.tableData = data.map(parameterList => {
            return {
              id: parameterList.id,
              name: parameterList.name,
              displayName: parameterList.display_name,
              unit: parameterList.unit,
              description: parameterList.description,
              createdAt: moment(parameterList.created_at).format(MomentFormat),
              updateAt: moment(parameterList.update_at).format(MomentFormat)
            };
          });
        })
        .catch(error => Message.error(parseError(error)))
        .finally(() => (this.searchForm.btnLoading = false));
    },
    showEdit(row) {
      this.$refs.editComponent.showEditDialog(row);
    },
    remove(id, name) {
      MessageBox.confirm(`确定删除设备参数${name}吗？`, '删除提示', {
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(() => {
          deleteDeviceParameterApi(id)
            .then(() => Message.success('删除设备参数成功'))
            .catch(error => Message.error(parseError(error)))
            .finally(() => this.search());
        })
        .catch(() => {});
    },
    pageSizeChangeHandler() {
      this.currentPage = 1;
      this.search();
    },
    currentPageChangeHandler() {
      this.search();
    }
  }
};
</script>
<style lang="scss" scoped>
.device-parameter-container {
  padding: 10px;
}
</style>
